package cn.JHai.rbac.service.imp;

import cn.JHai.rbac.domain.Role;
import cn.JHai.rbac.mapper.RoleMapper;
import cn.JHai.rbac.query.PageResult;
import cn.JHai.rbac.query.QueryObject;
import cn.JHai.rbac.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleImp implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    public int Delete(Long id) {
        roleMapper.deleteRelation(id);
        return roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public Role get(Long id) {
        return roleMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Role> findAll() {
        return roleMapper.selectAll();
    }

    @Override
    public int saveOrUpdata(Role role,Long ids[]) {
        //是插入
        if(role.getId()==null){
            roleMapper.insert(role);
        }else {
            //跟新
            roleMapper.deleteRelation(role.getId());
            roleMapper.updateByPrimaryKey(role);

        }
        System.out.println(ids);
        if(ids!=null&&ids.length>0){
            for (int i = 0;i<ids.length;i++) {
                roleMapper.insertRoleAndPermissionRelation(role.getId(),ids[i]);
            }

        }
        return 1;
    }

    @Override
    public PageResult query(QueryObject queryObject) {

        Long count = roleMapper.selectForCount(queryObject);
        //List<Role> roles =null;
        /*if(count<=3l){
            //roles = roleMapper.selectAll();
        }
        else{
            //roles = roleMapper.pageSelect(queryObject);
        }*/
        return new PageResult(queryObject.getCurrentPage(),queryObject.getPageSize(),roleMapper.pageSelect(queryObject),count);
    }


}
